// 腾讯AxB
import request from '@/utils/request'
import { Message } from 'element-ui'

export function virtualNumber(calleeNumber, callerNumber, clueId, account_id, thirdSource, customerData) {
  // if ([0, '0'].includes(account_id) || !account_id) {
  //   const msg = 'account_id异常，请使用华为线路外呼!'
  //   Message.error(msg)
  //   return Promise.reject(msg)
  // }
  const data = {
    account_id, // 线索ID
    callee: calleeNumber, // 被叫号码，即线索号码
    caller: callerNumber // 主叫号码，必须为11位手机号
  }

  // CC对应需要调整：
  // 原本配置了哪些来源要调腾讯外呼，这个功能保持不变，在调腾讯外呼接口时，判断matchType的值
  // matchType=leadsId时，调腾讯外呼接口，将客资的clueId的值传到外呼接口leadsId上
  // matchType=outerLeadsId时，调腾讯外呼接口，将客资的clueId的值传到外呼接口outerLeadsId上
  let { matchType } = (customerData || {})
  if (typeof matchType === 'string') {
    matchType = matchType.toLowerCase()
  }
  switch (matchType) {
    case 'leadsId'.toLowerCase():
      data.leads_id = clueId
      break
    case 'outerLeadsId'.toLowerCase():
      data.outer_leads_id = clueId
      break

    default:
      data.leads_id = clueId
      break
  }
  return request({
    url: '/call-center/api/v1/qq/virtual-number',
    method: 'POST',
    data,
    hiddenError: true
  }).then(res => {
    const data = res.data
    if (![0, '0'].includes(data.code)) {
      return Promise.reject(data.message_cn || data.message || '调用腾讯接口失败!')
    }
    if (data.data.request_id) {
      return data.data
    } else {
      return Promise.reject(data.message_cn || data.message || '腾讯接口返回失败!')
    }
  }).catch(err => {
    const msg = err instanceof Error ? err.message : err
    Message.error(msg)
    return Promise.reject(err)
  })
}

